<template>
  <el-container class="home_container">
    <!-- 头部区域--S -->
    <el-header>
      <div class="header-left">
        <img src="@/assets/heima.png" alt="" />
        <span class="text">电商后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <!-- 头部区域--E -->
    <el-container>
      <!-- 侧边栏区域--S -->
      <el-aside :width="asideWidth">
        <AsideNav
          :menuNavList="menuList"
          @collapse="$event ? (asideWidth = '64px') : (asideWidth = '200px')"
        />
      </el-aside>
      <!-- 侧边栏区域--E -->
      <!-- 主体区域--S -->
      <el-main>
        <router-view />
      </el-main>
      <!-- 主体区域--E -->
    </el-container>
  </el-container>
</template>

<script>
import { getMenusAPI } from '@/api'
import { removeItem } from '@/utils/storage'
import AsideNav from './components/AsideNav.vue'
export default {
  name: 'HomeIndex',
  data() {
    return {
      menuList: [],
      asideWidth: '200px'
    }
  },
  created() {
    this.loadingMenus()
  },
  methods: {
    logout() {
      removeItem('token')
      this.$router.push({ name: 'login' })
    },
    // 加载左侧菜单栏
    async loadingMenus() {
      try {
        const { data } = await getMenusAPI()
        // console.log(data)
        if (data.meta.status !== 200) {
          return this.$message.error('左侧菜单栏数据获取失败')
        }
        this.menuList = data.data
      } catch (err) {
        this.$message.error('显示左侧菜单栏发生错误，请刷新重试')
      }
    }
  },
  components: {
    AsideNav
  }
}
</script>

<style scoped lang="less">
.home_container {
  height: 100%;
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #373d41;
    padding-left: 0;
    .header-left {
      display: flex;
      align-items: center;
      color: #fff;
      font-size: 24px;
      .text {
        padding-left: 15px;
      }
    }
  }
  .el-container {
    .el-aside {
      background-color: #333744;
    }
    .el-main {
      background-color: #eaedf1;
    }
  }
}
</style>
